#!/bin/bash
pushd ~/
function baihu()
{
    cat <<fox
    
***********************************
  白狐_Yunzai-Bot_V3安装程序                 

    1.安装Yunzai-Bot_v3
        
    2.安装喵喵和逍遥插件
             
    3.安装ffmpeg
    
    4.安装python3.10.8和pip和poetry
    
    5.Yunzai-Bot管理
        
    6.插件管理[可安装插件]
   
    0.退出安装程序
    
***********************************               
注意：脚本完全免费，禁止倒卖操作                   
此脚本作者为白狐
[QQ:1522099983[Q群:914418311]
项目地址:
https://gitee.com/baihu433/termux-Yunzai
***********************************
fox
}

function hubai()		
{
    read -p "请输入您需要操作的项目: " number
    case $number in
        1)
            yz
            ;;
        2)
            mx
            ;;
        3)
            ffmpegbak
            ;;
        4)
            pypip
            ;;
        5) 
            management
            ;;
        6)
            plugin
            ;;
        7)
            error
            ;;
        0)
            exit 0
            ;;

    esac
}

function yz()
{
        # 更新软件源和软件
        echo '更新软件';
        apt update
        apt upgrade -y
        echo '更新成功';
        echo
        
        # 安装git curl
        echo '安装git curl';
        apt-get install git curl wget -y
        echo 'wget git curl安装完成';
        echo

        # 安装nodejs
        echo '安装nodejs开始';
        bash <(curl -sL https://gitee.com/baihu433/termux-Yunzai/raw/master/setup_16.x)
        apt-get install -y nodejs
        npm -v
        if [ $? -ne 0 ]; then
        echo 'nodejs安装失败，请检查网络'
          exit
        else
          echo 'nodejs安装成功'
        fi
        echo        
        
        #安装pnpm
        echo '安装pnpm开始';
        npm config set registry http://registry.npm.taobao.org/
        npm install -g npm
        npm install -g pnpm
        pnpm -v
        if [ $? -ne 0 ]; then
        echo 'pnpm安装失败，请检查网络'
          exit
        else
          echo 'pnpm安装成功'
        fi
        echo
        
        # 安装并运行redis
        echo '安装redis开始';
        apt-get install redis -y
        redis-server --daemonize yes
        echo '安装redis完成';
        echo

        # 安装chromium
        echo '安装chromium开始';
        apt install chromium-browser -y
        echo '安装chromium完成';
        echo
        
        #安装中文字体
        echo '安装中文字体开始'
        apt-get install -y --force-yes --no-install-recommends fonts-wqy-microhei 
        apt-get install -y --force-yes --no-install-recommends ttf-wqy-zenhei
        echo '安装中文字体完成'

        # 克隆项目
        echo '克隆Yunzai-Bot开始';
        pushd ~/
        if [ ! -d "Yunzai-Bot" ];then
          git clone --depth=1 https://gitee.com/Le-niao/Yunzai-Bot.git
          if [ ! -d "Yunzai-Bot/" ];then
            echo "克隆失败"
            exit 0
          else
            echo '克隆完成'
          fi
        else
          echo '克隆完成'
        fi
        echo
        
        #安装Yunzai依赖
        echo '安装Yunzai-依赖'
        pushd Yunzai-Bot
        pnpm install -P
        pnpm ls oicq
        if [ $? -ne 0 ]; then
        echo '依赖安装失败，请检查网络'
          exit
        else
          echo '依赖安装成功'
        fi
        pushd ~/
        echo
        
        #写入自动命令
        echo '写入一键启动帮助'
        echo '正在将启动写入启动命令'
        echo echo 正在启动Yunzai-Bot > /usr/bin/yz 
        sed -i -e '1a echo 白狐制作 禁止倒卖' /usr/bin/yz 
        sed -i -e '2a redis-server --daemonize yes --save 900 1 --save 300 10 && pushd ~/Yunzai-Bot && node app' /usr/bin/yz 
        chmod 777 /usr/bin/yz
        echo echo 正在打开Yunzai-Bot后台日志 > /usr/bin/yzlog
        sed -i -e '1a echo 白狐制作 禁止倒卖' /usr/bin/yzlog 
        sed -i -e '2a pushd ~/Yunzai-Bot && pnpm run log' /usr/bin/yzlog 
        chmod 777 /usr/bin/yzlog 
        echo echo 正在打开Yunzai-Bot登录配置 > /usr/bin/yzlogin 
        sed -i -e '1a echo 白狐制作 禁止倒卖' /usr/bin/yzlogin  
        sed -i -e '2a pushd ~/Yunzai-Bot && pnpm run login' /usr/bin/yzlogin
        chmod 777 /usr/bin/yzlogin 
        echo echo 正在打开安装脚本 > /usr/bin/yzaz
        sed -i -e '1a echo 白狐制作 禁止倒卖' /usr/bin/yzaz
        sed -i -e '2a bash <(curl -sL https://gitee.com/baihu433/termux-Yunzai/raw/master/yzaz)' /usr/bin/yzaz
        chmod 777 /usr/bin/yzaz
        echo 'echo 正在停止Yunzai-Bot后台运行' > /usr/bin/yzstop
        sed -i -e '1a echo 白狐制作 禁止倒卖' /usr/bin/yzstop
        sed -i -e '2a pushd ~/Yunzai-Bot && pnpm stop' /usr/bin/yzstop
        chmod 777 /usr/bin/yzstop
        echo -e '\033[44;37m 启动,日志,停止,登录 命令配置成功 \033[0m'
        pushd ~/
        echo
}

function pypip()
{       
        pushd ~/
        echo '开始安装python3.10.8'
        sleep 5s
        #安装python3.10.8依赖
        echo '安装python3.10.8依赖'
        apt-get install gcc g++ make cmake build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl -y
        pushd ~/
        echo '正在下载python3.10.8源码'
        git clone --depth=1 https://gitee.com/baihu433/python3.10.8.git
        mv python3.10.8/Python-3.10.8.tar.xz ~/
        rm -rf python3.10.8
        rm -rf python3.10.8
        #解压Python-3.10.8.tar.xz
        echo '解压Python-3.10.8源码'
        tar xvJf Python-3.10.8.tar.xz
        rm -rf Python-3.10.8.tar.xz
        rm -rf Python-3.10.8.tar.xz
        #进入Python-3.10.8源码文件夹
        pushd Python-3.10.8
        # 开始编译python3.10.8源码
        echo '开始编译python3.10.8源码'
        ./configure --prefix=/usr/local/python3/
        echo '编译完成'
        # 开始安装python3.10.8
        echo '开始安装python3.10.8'
        make && make install
        echo '安装完成'
        # 添加环境变量
        echo '添加环境变量'
        PATH=/usr/local/python3/bin:$PATH
        #更改python2.7名称。
        pushd /usr/bin
        mv python python.bak
        mv pip pip.bak
        mv poetry poetry.bak
        pushd ~/
        #删除源码文件
        rm -rf Python-3.10.8
        rm -rf python-3.10.8
        #添加软链接
        ln -s /usr/local/python3/bin/python3 /usr/bin/python
        ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
        #更换pip默认下载源，来提升下载速度
        echo '更换pip下载源'
        pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/
        echo '更换完成'
        echo '更新pip'
        python -m pip install --upgrade pip
        pip --version
        echo '开始安装poetry'
        curl -sSL https://install.python-poetry.org | python -
        #添加软链接
        ln -s ~/.local/bin/poetry /usr/bin/poetry
        poetry --version
        echo
}



function ffmpegbak()
{ 
       architecture=$(uname -m)

 if [ "$architecture" = "aarch64" ]; then
        pushd ~/
        git clone --depth=1 https://gitee.com/baihu433/ffmpeg-arm64.git
        mv ~/ffmpeg-arm64/ffmpeg /usr/local/bin
        ln -s /usr/local/bin/ffmpeg /usr/bin/ffmpeg
        chmod +x /usr/local/bin/ffmpeg
        ffmpeg
        echo '安装完成'
        rm -rf ffmpeg-arm64
        rm -rf ffmpeg-arm64
 elif [ "$architecture" = "x86_64" ]; then
        pushd ~/  
        git clone --depth=1 https://gitee.com/baihu433/ffmpeg-amd6.git
        mv ~/ffmpeg-amd64/ffmpeg /usr/local/bin
        ln -s /usr/local/bin/ffmpeg /usr/bin/ffmpeg
        chmod +x /usr/local/bin/ffmpeg
        ffmpeg
        echo '安装完成'
        rm -rf ffmpeg-amd64
        rm -rf ffmpeg-amd64
    else
       echo "师傅 您干什么的呀？"
fi
}
function mx()
{       
        pushd ~/Yunzai-Bot
        echo '安装喵喵插件'
        git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/
        echo '安装喵喵插件依赖'
        pnpm add image-size axios -w
        echo
        echo '安装逍遥插件'
        git clone --depth=1 https://gitee.com/Ctrlcvs/xiaoyao-cvs-plugin.git ./plugins/xiaoyao-cvs-plugin/
        echo '安装逍遥插件依赖'
        pnpm add superagent promise-retry -w
        pushd ~/
        echo '完成'
        echo
}

function management()
{
        pushd ~/
        bash <(curl -sL https://gitee.com/baihu433/termux-Yunzai/raw/master/management)
        pushd ~/
}

function plugin()
{
        pushd ~/
        bash <(curl -sL https://gitee.com/baihu433/termux-Yunzai/raw/master/pluginoptions)
        pushd ~/
}

function mainbak()
{
    while true
    do
        baihu
        hubai
    done
}
mainbak